<?php
namespace app\common\model;
use app\common\model\Common;
use \think\Loader;

// +----------------------------------------------------------------------
// | XpCms
// +----------------------------------------------------------------------
// | Copyright (c) 2021-2099
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Mercurio（Xp） <725668993@qq.com>
// +----------------------------------------------------------------------

/*
** 文章分类表 模型
*/

class Friendlink extends Common {
	
	// 注册模型事件
	protected static function init() {
		// 新增后事件
		Friendlink::afterInsert(function($_friendlink) {
			// 获取传入的数据
			$data = $_friendlink -> data;
			
			
		});
	}
	
	// 获取分类列表
	public function friendlinkLists(
	    $where, 		// 查询条件
		$nowPage 	= 1, 			// 当前页
		$pagesize 	= 10 			// 每页条数       
    ) {
        
        // 计算页数
		$total = $this -> where($where) -> count();
		$allPages = (int) ceil($total / $pagesize);
		// 验证输入的页码
		if($nowPage > $allPages){
			$nowPage = 1;
		}
		
		// 分页
		$pageStart = ($nowPage - 1) * $pagesize;
		$pageLimit = $pagesize;
		
		$lists = $this -> where($where)
		                -> order('sort', 'asc')
						->paginate($pagesize);
		$page = $lists->render();
		
		return array(
				'lists' 	=> $lists,
				'page'      => $page,
				'total' 	=> $total,
				'allpages' 	=> $allPages,
				'nowpage' 	=> $nowPage
			);
	}
	
	// 获取单个友链详情
	public function getFriendlinkDetail($id) {
		$lists = $this -> find($id);
		
		if($lists){
			return $lists;
		}
		
		return false;
	}
	
	
	// 编辑状态
	public function editStatus($id) {
		$lists = $this -> where('id', '=', $id) -> find();
		
		if($lists){
			$status = $lists['status'] ? 0 : 1;
			
			$result = $this -> save(array('status' => $status), array('id' => $id));
			if($result){
				return true;
			}
		}
		return false;
	}
	
	// 删除分类
	public function deletes($id) {
		// 验证 id
		$find = $this -> where(array('id' => $id)) -> value('id');
		if(!$find){
			return array('status' => 0, 'msg' => '参数错误');
		}
		
		// 执行删除
		if($this -> where(array('id' => $id)) -> delete()){
			return array('status' => 1, 'msg' => '删除成功');
		}
		return array('status' => 0, 'msg' => '删除失败');
	}
	
}
